package com.rabbitmqspringboot.controller;


import com.rabbitmqspringboot.constant.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/producer")
@RestController
public class producerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 工作队列
     * @return
     */
    @RequestMapping("/work")
    public String work() {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE, "hello work message"+i);
        }

        return "工作队列信息发送成功：ok";
    }

    /**
     * 广播队列
     * @return
     */
    @RequestMapping("/fanout")
    public String fanout() {
        rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE,"", "hello fanout message");
        return "广播队列信息发送成功：ok";
    }

    /**
     * 路由模式
     * @return
     */
    @RequestMapping("/direct/{routeKey}")
    public String direct(@PathVariable("routeKey") String routeKey) {
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE,routeKey, "direct message is "+routeKey);
        return "主题队列信息发送成功：ok";
    }

    /**
     * 通配符模式
     */
    @RequestMapping("/topic/{routeKey}")
    public String topic(@PathVariable("routeKey") String routeKey) {
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE,routeKey, "topic message is "+routeKey);
        return "通配符队列信息发送成功：ok";
    }



}
